<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;

class ShopOrderSplit extends CommonModel {
	protected $table = 'shop_order_split';
	//服务商订单确认付款后更新
    public function updateServiceOrder($order_sn,$order_amount){
        $order_split_info = static::where(['order_sn'=>$order_sn])->first();
        $company_proportion = Config::key('company_proportion')->value('value'); //物业分成比例
        $platform_proportion = Config::key('platform_proportion')->value('value'); //平台分成比例暂时为0

        $village_id = ShopStore::getVillageIdByStore($order_split_info->store_id);
        if(!$village_id){
            //平台自营
            $split_uid=0;
            $store_amount = $order_amount;
            $platform_amount = $terrace_amount=0;
        }else {
            $terrace_amount = bcdiv(floor(bcmul($order_amount,$company_proportion,2)),100); //分成金额
            $platform_amount = bcdiv(floor(bcmul($order_amount,$platform_proportion,2)),100); //平台分成金额
            $store_amount = bcsub(bcsub($order_amount,$terrace_amount,2),$platform_amount,2);
            $split_uid = ClientUserRelationship::where(['village_id' => $village_id])->value('uid');
        }
        $order_split_info=array(
            'order_sn'=>$order_sn,
            'split_uid'=>$split_uid,
            'order_amount'=>$order_amount,
            'refund_amount'=>0,
            'store_amount'=>$store_amount,
            'platform_amount'=>$platform_amount,
            'terrace_amount'=>$terrace_amount,
            'status'=>0,
        );

        $create=ShopOrderSplit::where(['order_sn'=>$order_sn])->update($order_split_info);
        if(!$create){
            throw new Exception('订单结算表生成失败');
        }
    }

	//确认订单与退款申请延时
    public function confirmOrder($order_sn){
        $confirm_at = Config::key('confirm_time')->value('value');
        $confirm_at = date('Y-m-d'.' 00:00:30',time()+$confirm_at*3600*24);
        self::where(['order_sn'=>$order_sn])->update(['status'=>1,'confirm_at'=>$confirm_at]);
    }

    /**
     * 所有退款
     * @param $order_sn
     * @param $refund_amount
     */
	public function refundAll($order_sn,$refund_amount){
        self::where(['order_sn'=>$order_sn])->update(['status'=>3,'refund_amount'=>$refund_amount,'store_amount'=>0,'terrace_amount'=>0,'platform_amount'=>0]);
    }

    //退款申请延时
    public function refund($order_sn){
        $confirm_at = Config::key('confirm_time')->value('value');
        $refund_confirm_at = Config::key('confirm_refund_time')->value('value');
        $confirm_at = date('Y-m-d'.' 00:00:30',time()+($confirm_at+$refund_confirm_at)*3600*24);
        self::where(['order_sn'=>$order_sn])->update(['status'=>1,'confirm_at'=>$confirm_at]);
    }

    //退款处理成功
    public function refund_success($order_sn,$refund_amount){
        $split_info = self::where(['order_sn'=>$order_sn])->first();
        if($split_info->status!=1){
            throw new \Exception('该订单已结算，无法退款');
        }
        $amount =bcadd ($split_info['refund_amount'],$refund_amount,2);

        if($amount>=$split_info->order_amount){
            self::refundAll($order_sn,$split_info->order_amount);
        }
        $confirm_at = Config::key('confirm_time')->value('value');
        $company_proportion = Config::key('company_proportion')->value('value');
        $platform_proportion = Config::key('platform_proportion')->value('value');

        $surplus_amount = bcsub($split_info->order_amount,$amount,2);  //退款后剩余金额
        $terrace_amount = bcdiv(floor(bcmul($surplus_amount,$company_proportion,2)),100); //分成金额
        $platform_amount = bcdiv(floor(bcmul($surplus_amount,$platform_proportion,2)),100); //分成金额
        $store_amount = bcsub(bcsub($surplus_amount,$terrace_amount,2),$platform_amount,2);//商家金额
        $update=array(
            'confirm_at'=>date('Y-m-d'.' 00:00:30',time()+$confirm_at*3600*24),
            'refund_amount'=>$amount,
            'store_amount'=>$store_amount,
            'status'=>1,
            'platform_amount'=>$platform_amount,
            'terrace_amount'=>$terrace_amount,
        );
        self::where(['order_sn'=>$order_sn])->update($update);
    }
}